#!/usr/bin/env python

# *************************************************************************

# Copyright 2010 Texas A&M University, 
#                College Station, Texas
#                Contact: R Crosby (rwc@cs.tamu.edu)
#
# Copyright 2003, 2004 by BiRC -- Bioinformatics Research Center
#                University of Aarhus, Denmark
#                Contact: Thomas Mailund <mailund@birc.dk>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# *************************************************************************

import getopt
import sys
import os
import os.path
   
# *************************************************************************

def ParseCommandLine():
    
    try:
        opts, tfiles = getopt.getopt(sys.argv[1:],
                                   "hvw:c:n:i:s:",
                                   ["help", "verbose", "work-dir=", "job-count=", "job-name=", "job-id=", "segments="] )
    except getopt.GetoptError, err:
        print str(err)
        Usage()
        sys.exit(2)

    opt = { 'verbose': False }

    for key, val in opts:
        if key in ("-v", "--verbose"):
            opt['verbose'] = True
        elif key in ("-h", "--help"):
            Usage()
            sys.exit()
        elif key in ("-w", "--work-dir"):
            opt['workdir'] = val
        elif key in ("-c", "--job-count"):
            opt['jobcount'] = int(val)
        elif key in ("-n", "--job-name"):
            opt['jobname'] = val
        elif key in ("-i", "--job-id"):
            opt['jobid'] = val
        elif key in ("-s", "--segments"):
            opt['segments'] = val

    if not opt.has_key('workdir'):
        print 'Work directory option required'
        sys.exit(2)

    if not opt.has_key('jobcount'):
        print 'Job count option required'
        sys.exit(2)

    if not opt.has_key('jobname'):
        print 'Job name option required'
        sys.exit(2)

    if not opt.has_key('jobid'):
        print 'Job id option required'
        sys.exit(2)

    return opt

# *************************************************************************

def Usage():
    print '''
pqqback - Back end to QuickQuartet for cluster based parallel processing

** Typically the user will not be responsible for running this script. It
   will be auto-generated by the pqq front end script!

Usage:

pqq parms

Parms:

   --verbose        (-v) - Be loquacious
   --help           (-h) - Print this message
   --work-dir=s     (-w) - Work directory containing output files
   --job-count      (-c) - Number of jobs in batch
   --job-name       (-n) - Job name prefix for all jobs in the batch

'''

# *************************************************************************

if __name__ == "__main__":
    opt, tfiles = ParseCommandLine()
    print opt
